﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DbMapping.Common;
using VideoCache.Server.BLL;
using VideoCache.Server.Model;
using VideoCache.Server.MVCWeb.Areas.Admin.Models;
using VideoCache.Server.MVCWeb.Attributes;

namespace VideoCache.Server.MVCWeb.Areas.Admin.Controllers
{
    [ActionAuthorize(AuthorizeType.Administrator)]
    public class SiteChannelController : Controller
    {
        //
        // GET: /Admin/SiteChannel/
        public ActionResult Index()
        {
            Orders orders = new Orders();
            orders.Add(new OrderItem(SiteChannelInfo.SiteIdField, OrderDirection.Asc));
            var list = SiteChannelBLL.FindAll(null, orders);

            return View(list);
        }

        public ActionResult Create()
        {
            SiteChannelModel model = new SiteChannelModel();

            ViewBag.SiteId = new SelectList(SiteBLL.FindAll(null, null), "Id", "DisplayName");

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(SiteChannelModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    SiteChannelInfo info = new SiteChannelInfo();
                    info.Name = model.Name;
                    info.SiteId = model.SiteId;
                    info.Domain = model.Domain;
                    info.UrlPattern = model.UrlPattern;
                    info.Enable = model.Enable;
                    info.Assembly = model.Assembly;
                    info.CreateTime = DateTime.Now;
                    info.ModifyTime = DateTime.Now;

                    SiteChannelBLL.Add(info);

                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Error", ex.Message);
                }
            }

            return View(model);
        }

        public ActionResult Edit(int id)
        {
            SiteChannelInfo info = SiteChannelBLL.Find(id);

            if (info != null)
            {
                SiteChannelModel model = new SiteChannelModel();
                model.Id = info.Id;
                model.Name = info.Name;
                model.Domain = info.Domain;
                model.UrlPattern = info.UrlPattern;
                model.SiteId = info.SiteId;
                model.Enable = info.Enable;
                model.Assembly = info.Assembly;

                ViewBag.SiteId = new SelectList(SiteBLL.FindAll(null, null), "Id", "DisplayName", info.SiteId);

                return View(model);
            }

            return HttpNotFound();
        }

        [HttpPost]
        public ActionResult Edit(SiteChannelModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    SiteChannelInfo info = SiteChannelBLL.Find(model.Id);
                    if (info == null) return HttpNotFound();

                    info.Name = model.Name;
                    info.SiteId = model.SiteId;
                    info.Domain = model.Domain;
                    info.UrlPattern = model.UrlPattern;
                    info.Enable = model.Enable;
                    info.Assembly = model.Assembly;
                    info.ModifyTime = DateTime.Now;

                    SiteChannelBLL.Update(info);

                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Error", ex.Message);
                }
            }

            return View(model);
        }

        public ActionResult Delete(int id)
        {
            SiteChannelBLL.Delete(id);

            return RedirectToAction("Index");
        }
    }
}